package com.bc004346.travelus.Model.Abstract;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bc004346.travelus.Fragment.NavigationDrawerFragment;
import com.bc004346.travelus.R;
import com.bc004346.travelus.Utility.Utility;

import static com.bc004346.travelus.Utility.Utility.onActivityStart;
import static com.bc004346.travelus.Utility.Utility.onActivityStop;

/**
 * Created by Renats on 9/4/2014.
 * Generic fragment container
 */
public abstract class SingleFragmentActivity extends FragmentActivity {

    protected NavigationDrawerFragment mNavigationDrawer;
    protected ProgressBar mTopProgressBar;
    protected TextView mStatus;
    protected Menu mMenu;

    protected abstract Fragment getFragment();

    @Override
    protected void onStart() {
        super.onStart();
        onActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        onActivityStop(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        try {
            getMenuInflater().inflate(R.menu.action_item_menu, menu);
            mMenu = menu;
        } catch (Exception e) {
            Utility.log(e.toString());
            mStatus.setText(e.getMessage());
        }
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mNavigationDrawer.handleOnPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mNavigationDrawer.syncState();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        mTopProgressBar = (ProgressBar) findViewById(R.id.activityFragmentProgressBar);
        mStatus = (TextView) findViewById(R.id.activityFragmentStatusTextView);
        mNavigationDrawer = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.fullscreenDrawer);
        if (mNavigationDrawer != null) mNavigationDrawer.init();
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            fragment = getFragment();
            fm.beginTransaction()
                    .add(R.id.fragmentContainer, fragment)
                    .commit();
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mNavigationDrawer.syncState();
    }

    protected void toggleMenuState(boolean enabled) {
        try {
            for (int index = 0; index < mMenu.size(); index++) {
                MenuItem item = mMenu.getItem(index);
                item.setEnabled(enabled);
            }
        } catch (Exception e) {
            Utility.log(e.toString());
            mStatus.setText(e.getMessage());
        }
    }

}
